package com.yxt.common.http;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.yxt.common.sys.CommConstant;
import com.yxt.common.xml.SAXHandler;

public class HttpUrlExtract {

	public static List<Map<String, String>> doGet(String addr,
			Map<String, String> params) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			URL url = new URL(getHttpUrl(addr, params));
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			SAXParserFactory factory = SAXParserFactory.newInstance();

			SAXParser parser = factory.newSAXParser();
			SAXHandler handler = new SAXHandler();
			parser.parse(conn.getInputStream(), handler);
			conn.disconnect();
			list.addAll(handler.getXmlContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public static List<Map<String, String>> getLocalXmlContent(String url) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			SAXHandler handler = new SAXHandler();
			parser.parse(new FileInputStream(url), handler);
			list.addAll(handler.getXmlContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	private static String getHttpUrl(String addr, Map<String, String> params) {
		StringBuffer url = new StringBuffer();
		url.append(addr);
		if (params != null && !params.isEmpty()) {
			Iterator<Entry<String, String>> iterator = params.entrySet()
					.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> entry = iterator.next();
				url.append("&");
				url.append(entry.getKey());
				url.append("=");
				url.append(entry.getValue());
			}
		}
		return url.toString();
	}
	
	public static boolean checkUrlIsValidate(String addr) {
		boolean validate = true;
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			if (conn.getResponseCode() != 200) {
				validate = false;
			}
		} catch (Exception e) {
			validate = false;
		}
		return validate;
	}

	public static void downloadBitmap(String addr,String name) {
		String fname = CommConstant.Config.DEFAULT_PHONE_DIR+CommConstant.Config.PHOTO_LOCATION+name;
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream input = conn.getInputStream();
			OutputStream output = new FileOutputStream(fname);
			int r;
			byte[] bytes = new byte[1024];
			while((r=input.read(bytes)) != -1){
				output.write(bytes, 0, r);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
